package net.pimpas.core.visitor;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import net.pimpas.util.EqualsHelper;

import org.apache.commons.lang.builder.HashCodeBuilder;

public abstract class BasePackageVisitor implements PackageVisitor {
	
	private Collection<String> allowedPackages;
	
	public BasePackageVisitor() {
		this.setAllowedPackages(new HashSet<String>());
	}
	
	public BasePackageVisitor(String... directories) {
		this();
		this.getAllowedPackages().addAll(Arrays.asList(directories));
	}

	public boolean accept(Package pack) {
		if(this.getAllowedPackages().size() == 0) return true;
		for(String pkg: this.getAllowedPackages()) {
			if(pack.getName() != null && pack.getName().equals(pkg)) return true;
		}
		
		return false;
	}

	public Collection<String> getAllowedPackages() {
		return allowedPackages;
	}

	public void setAllowedPackages(Collection<String> allowedPackages) {
		this.allowedPackages = allowedPackages;
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.getAllowedPackages()).toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		return EqualsHelper.equals(this, obj);
	}
}